package com.vavr.checkincms.configuration;

import com.vavr.checkincms.constant.CheckinConstants;
import com.vavr.checkincms.filter.CheckinSessionFilter;
import com.vavr.checkincms.mapper.CheckinRecordsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.servlet.ConditionalOnMissingFilterBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;

/**
 * CheckinConfiguration
 *
 * @since 2.0.0
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
public class CheckinConfiguration {

    /**
     * CheckinSessionFilter
     *
     * @param properties {@link CheckinProperties}
     * @param handlerExceptionResolver {@link HandlerExceptionResolver}
     * @return FilterRegistrationBean<CheckinSessionFilter>
     */
    @Bean
    @ConditionalOnMissingFilterBean(CheckinSessionFilter.class)
    public FilterRegistrationBean<CheckinSessionFilter> checkinSessionFilter(CheckinProperties properties, HandlerExceptionResolver handlerExceptionResolver) {
        FilterRegistrationBean<CheckinSessionFilter> registrationBean = new FilterRegistrationBean<>();
        CheckinSessionFilter checkinSessionFilter = new CheckinSessionFilter();
        checkinSessionFilter.setHandlerExceptionResolver(handlerExceptionResolver);

        registrationBean.setFilter(checkinSessionFilter);
        registrationBean.addInitParameter(CheckinConstants.WHITE_LIST_URL,
                properties.getSessionFilterConfig().getWhiteListUrl());
        registrationBean.setOrder(0);
        registrationBean.setName("checkinSessionFilter");
        return registrationBean;
    }
}
